#include <iostream>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <cstring>

using namespace std;

int main(){

    struct sockaddr_in serv_addr{};
    memset(&serv_addr,0,sizeof serv_addr);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    serv_addr.sin_port = ntohs(9520);

    while (1){
        int sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        connect(sock,(struct sockaddr*)&serv_addr,sizeof serv_addr);
        cout<<"请输入: ";
        char str[2048];
        cin>>str;
        if(str[0]=='q'){
            cout<<"退出成功"<<endl;
            send(sock,str,strlen(str) * sizeof(char),0);
            close(sock);
            break;
        }
        send(sock,str,strlen(str) * sizeof(char),0);
        char buffer[2];
        ssize_t size;
        while ((size = recv(sock, buffer, sizeof(buffer),0))>0){
            string s(buffer,0,size);
            cout << "读取" << size << "字节,内容：" << s << endl;
            shutdown(sock, SHUT_WR);
        }
    }

    return 0;
}